//
//  VisitorView.swift
//  TakeTime
//
//  Created by bachelor on 16/10/24.
//  Copyright © 2016年 bachelor. All rights reserved.
//

import UIKit
import Alamofire

protocol VisitorViewDelegate: NSObjectProtocol {
    func loginClick()
    func registerClick()
}
class VisitorView: UIView {
    
    weak var visitorDelegate: VisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        self.setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    ///  设置访客视图信息
    ///
    ///  :param: iconName 图标名称
    ///  :param: message  消息文字
    ///  :param: isHome   是否首页
    func setupInfo(iconName: String, message: String, isHome: Bool = false) {
        
        
        messageLabel.text = message
        messageLabel.sizeToFit()
        
        if isHome {
            homeIconView.image = UIImage(named: iconName)
            iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        } else {
            homeIconView.image = nil
            iconView.image = UIImage(named: iconName)
        }
    }
    ///  设置 UI 控件
    private func setupUI() {
        
        addSubview(iconView)
        addSubview(homeIconView)
        addSubview(maskIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 自动布局
        // 转圈
        iconView.translatesAutoresizingMaskIntoConstraints = false
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -40))
        // 小房子
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -40))
        // 消息文本
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 20))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[label(224)]", options: .alignAllLastBaseline, metrics: nil, views: ["label": messageLabel]))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[label(40)]", options: .alignAllLastBaseline, metrics: nil, views: ["label": messageLabel]))
        // 注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .left, relatedBy: .equal, toItem: messageLabel, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: messageLabel, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 20))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[btn(100)]", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["btn": registerButton]))
        // 登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .right, relatedBy: .equal, toItem: messageLabel, attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: messageLabel, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 20))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[btn(100)]", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["btn": loginButton]))
        // 遮罩视图
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[maskView]|", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["maskView": maskIconView]))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[maskView]-160-|", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["maskView": maskIconView]))
    }
    
    /// 开始动画
    func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        
        iconView.layer.add(anim, forKey: nil)
        
        
    }
    
    func register() {
        visitorDelegate?.registerClick()
        
        
    }
    func login() {
        visitorDelegate?.loginClick()
    }
    
    // MARK: - 界面元素懒加载
    /// 图标
    lazy var iconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()
    
    /// 首页小房子
    lazy var homeIconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
    }()
    
    /// 消息文本
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.textAlignment = NSTextAlignment.center
        label.font = UIFont.systemFont(ofSize: 14)
        label.numberOfLines = 0
        
        
        return label
    }()
    
    /// 注册按钮
    lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", for: UIControlState.normal)
        btn.setTitleColor(UIColor.orange, for: .normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        btn.addTarget(self, action: #selector(VisitorView.register), for: .touchUpInside)
        return btn
    }()
    
    /// 登录按钮
    lazy var loginButton: UIButton = { [weak self] in
        let btn = UIButton()
        btn.setTitle("登录", for: UIControlState.normal)
        btn.setTitleColor(UIColor.orange, for: .normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        btn.addTarget(self, action: #selector(VisitorView.login), for: .touchUpInside)
        return btn
    }()
    
    /// 遮罩图片
    lazy var maskIconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return UIImageView(image: image)
    }()
}
